<?php


namespace app\exception;


use support\exception\BusinessException;
use Tinywan\Jwt\Exception\JwtTokenException;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;
use Throwable;


class MyBusinessException extends ExceptionHandler
{
//    public function render(Request $request): ?Response
//    {
//        // json请求返回json数据
//        if ($request->expectsJson()) {
//            return json(['code' => $this->getCode() ?: 500, 'message' => $this->getMessage()]);
//        }
//        // 非json请求则返回一个页面
//        return new Response(200, [], $this->getMessage());
//    }

    public $dontReport = [
        BusinessException::class,
    ];

    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    public function render(Request $request, Throwable $exception): Response
    {
        if(($exception instanceof BusinessException) && ($response = $exception->render($request)))
        {
            return $response;
        }
        if(($exception instanceof JwtTokenException) )
        {
            return json(['code' => 401 , 'message' => $exception->getMessage()]);
        }
        if (config('app.debug')){
            return parent::render($request, $exception);
        }
        return json(['code' => 401 , 'message' => '系统错误']);

    }

}